package com.bit.springbook.controller;

import com.bit.springbook.constant.Constant;
import com.bit.springbook.model.BookInfo;
import com.bit.springbook.model.PageResult;
import com.bit.springbook.model.RequestPage;
import com.bit.springbook.model.Result;
import com.bit.springbook.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    // 添加图书
    @RequestMapping("/addBook")
    public Boolean addBook(@RequestBody BookInfo bookInfo) {
        log.info("BookController.addBook 添加图书 {}", bookInfo);
        // 校验
        if (bookInfo == null ||
            !StringUtils.hasLength(bookInfo.getBookName()) ||
            !StringUtils.hasLength(bookInfo.getAuthor()) ||
            bookInfo.getCount() == null ||
            bookInfo.getPrice() == null ||
            !StringUtils.hasLength(bookInfo.getPublish()) ||
            bookInfo.getStatus() == null
        ) {
            log.warn("图书校验失败");
            return false;
        };
        // 添加
        return bookService.addBook(bookInfo) != 0;
    }

    // 获取分页列表
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage, HttpSession session) {
        log.info("BookController.getListByPage 获取分页列表 {}", requestPage);
        if (session.getAttribute(Constant.USER_SESSION_KEY) == null) {
            return Result.noLogin();
        }
        PageResult<BookInfo> result = bookService.getListByPage(requestPage);
        return Result.success(result);
    }

    // 获取图书信息
    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer id) {
        log.info("获取图书信息 getBookInfo" + id);
        if (id == null) {
            log.warn("id 不能为空");
            return null;
        }
        try {
            BookInfo bookInfo = bookService.getBookInfo(id);
            return bookInfo;
        } catch (Exception e) {
            log.error("getBookInfo出现异常：", e);
            return null;
        }
    }

    // 更新图书信息
    @RequestMapping("/updateBook")
    public Boolean updateBook(BookInfo bookInfo) {
        log.info("更新图书 updateBook, {}", bookInfo);
        if (bookInfo == null) {
            log.warn("图书不能为空");
            return false;
        }
        try {
            Integer update = bookService.updateBook(bookInfo);
            if (update < 0) {
                log.error("更新失败");
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("更新出现异常 updateBook ", e);
            return false;
        }
    }

    // 删除图书
    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer id) {
        log.info("deleteBook 删除图书 " + id);
        if (id == null) {
            log.warn("id 不能为空");
            return false;
        }
        try {
            Boolean flag = bookService.deleteBook(id);
            return flag;
        } catch (Exception e) {
            log.error("删除出现异常：", e);
            return false;
        }
    }

    // 批量删除图书
    @RequestMapping("/deleteBatch")
    public Boolean deleteBatch(@RequestParam List<Integer> ids) {
        log.info("deleteBatch 删除图书 {}", ids);
        if (ids == null) {
            log.warn("ids 不能为空");
            return false;
        }
        try {
            Boolean flag = bookService.deleteBatch(ids);
            return flag;
        } catch (Exception e) {
            log.error("删除出现异常：", e);
            return false;
        }
    }
}
